﻿Imports System.Xml.Serialization
Imports System.Collections.ObjectModel

''' <summary>
''' 自定义语言字典
''' </summary>
''' <remarks></remarks>
  <Serializable()>
  Public Class Dictionary
    Inherits ObservableCollection(Of ChinesePartItem)
    Implements IXmlSerializable, ICloneable



    Public Sub New()

    End Sub
  
    Public Sub New(ByVal collection As IEnumerable(Of ChinesePartItem))
        MyBase.New(collection)

    End Sub


    Public Overloads Sub Add(simplified As String, traditional As String)
        MyBase.Add(New ChinesePartItem With {.Simplified = simplified, .Traditional = traditional})

    End Sub



#Region "Clone 实现"

    Function Clone() As Dictionary
        Return New Dictionary(Me)
    End Function

    Private Function ImeClone() As Object Implements System.ICloneable.Clone
        Return Me.Clone
    End Function

#End Region


    Public Function GetSchema() As System.Xml.Schema.XmlSchema Implements System.Xml.Serialization.IXmlSerializable.GetSchema
        Return Nothing
    End Function

    Public Sub ReadXml(reader As System.Xml.XmlReader) Implements System.Xml.Serialization.IXmlSerializable.ReadXml
        Dim xml = XElement.Load(reader)

        Dim items = xml...<Item>.Select(Function(x) (New With {.Simplified = x.@Simplified, .Traditional = x.@Traditional}))



        For Each i In items
            If Not String.IsNullOrEmpty(i.Simplified) AndAlso
                Not String.IsNullOrEmpty(i.Traditional) Then
                Me.Add(i.Simplified, i.Traditional)
            End If
        Next


    End Sub

    Public Sub WriteXml(writer As System.Xml.XmlWriter) Implements System.Xml.Serialization.IXmlSerializable.WriteXml
        Dim xml = <Dictionary>
                      <%= From item In Me
                          Select <Item Simplified=<%= item.Simplified %> Traditional=<%= item.Traditional %>></Item>
                      %>
                  </Dictionary>


        xml.WriteTo(writer)

    End Sub
End Class


''' <summary>
''' 中文简繁体对项
''' </summary>
''' <remarks></remarks>
Public Class ChinesePartItem

    ''' <summary>
    ''' 简体字符串
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Property Simplified As String


    ''' <summary>
    ''' 繁体字符串
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Property Traditional As String


    Public Overrides Function ToString() As String
        Return String.Format("{0}  <->  {1}", Me.Simplified, Me.Traditional)
    End Function

End Class